#include "../s21_decimal.h"

int s21_truncate_private(s21_decimal *source, s21_decimal *destination) {
  int discarded_digit = 0;
  int discarded_digits_sum = 0;
  int scale = s21_get_scale(source);

  if (scale == 0) {
    s21_copy(source, destination);
  } else {
    s21_long_decimal value = INIT_ZERO_S21_LONG_DECIMAL;
    s21_extend(source, &value);
    s21_set_long_scale(&value, 0);

    for (int i = 0; i < scale; i++) {
      discarded_digit = s21_div_long_by_ten(&value);
      discarded_digits_sum += discarded_digit;
    }

    s21_reduce(&value, destination);
  }

  return discarded_digit + discarded_digits_sum * 10;
}

void s21_increase_by_one(s21_decimal *value) {
  for (int i = 0; i <= MAX_INDEX; i++) {
    if (s21_get_bit(value, i) == 1) {
      s21_reset_bit(value, i);
    } else {
      s21_set_bit(value, i);
      i = MAX_INDEX;  // break;
    }
  }
}